package com.stack.knowyouservice.service.impl;

import com.stack.knowyoubase.anotional.TimeOut;
import com.stack.knowyoubean.bean.HomeWork;
import com.stack.knowyoudao.dao.HomeWorkDao;
import com.stack.knowyouservice.service.HomeWorkService;
import com.stack.knowyoubase.util.PageUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 作业表服务层
 *
 * @author stack
 */
@Service
@Scope("singleton")
public class HomeWorkServiceImpl implements HomeWorkService {

    @Resource
    private HomeWorkDao homeWorkDao;
    @Resource
    private PageUtil pageUtil;


    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public HashMap<String, Object> homeworkSearch(String hname, Integer hkid, Integer hstatus, Integer page, Integer limit, Integer sid) {

        HashMap<String, Integer> pageMap = pageUtil.pageCreate(page, limit);
        //hname检验在前
        if (!StringUtils.isEmpty(hname)) {
            hname = "%" + hname + "%";
        }
        //数量查询
        Integer count = homeWorkDao.findHomeWorkCount(sid, hkid, hstatus, hname);
        HashMap<String, Object> homeworkMap = new HashMap<>(2);
        if (count == 0) {
            //解决空指针异常
            homeworkMap.put("count", count);
            homeworkMap.put("homeworkList", null);
        } else {
            List<HomeWork> homeWork = homeWorkDao.findHomeWork(sid, hkid, hstatus, hname, pageMap.get("page"), pageMap.get("limit"));
            homeworkMap.put("count", count);
            homeworkMap.put("homeworkList", homeWork);
        }
        return homeworkMap;
    }

    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public HomeWork homeWorkInfo(Integer hid, Integer sid) {
        //判断该课程是否持有这个作业,课程号是安全的
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean contains = homeWorkBySid.contains(hid);
        if (!contains) {
            return null;
        } else {
            return homeWorkDao.findHomeWorkById(hid);
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean homeWorkInsert(HomeWork homeWork) {
        int i = homeWorkDao.insertHomeWork(homeWork);
        return i == 1;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean homeWorkUpdate(HomeWork homeWork, Integer sid) {
        //判断该课程是否持有这个作业,课程号是安全的
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean contains = homeWorkBySid.contains(homeWork.getHid());
        if (!contains) {
            return false;
        }
        //作业更新
        else {
            int i = homeWorkDao.updateHomeWork(homeWork);
            return i == 1;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean homeWorkUpdateStatus(Integer[] hids, Integer hstatus, Integer sid) {
        //判断是否包含更新状态的集合，如果不包含不进行更新返回更新失败
        List<Integer> hidsList = new ArrayList<>(Arrays.asList(hids));
        //去除集合空元素
        hidsList.removeAll(Collections.singleton(null));
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean b = homeWorkBySid.containsAll(hidsList);
        if (!b) {
            return false;
        } else {
            //待优化为批处理
            for (Integer hid : hidsList) {
                homeWorkDao.updateHomeWorkStatus(hid, hstatus);
            }
            return true;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean homeWorkDelete(Integer[] hids, Integer sid) {
        //判断是否包含更新状态的集合，如果不包含不进行更新返回更新失败
        List<Integer> hidsList = new ArrayList<>(Arrays.asList(hids));
        //去除集合空元素
        hidsList.removeAll(Collections.singleton(null));
        //判断是否包含更新状态的集合，如果不包含不进行更新返回更新失败
        List<Integer> homeWorkBySid = homeWorkDao.findHomeWorkBySid(sid);
        boolean b = homeWorkBySid.containsAll(hidsList);
        if (!b) {
            return false;
        } else {
            //待优化为批处理
            for (Integer hid : hidsList) {
                homeWorkDao.deleteHomeWork(hid);
            }
            return true;
        }
    }
}
